

package com.zdkj.subway.support.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.entity.SysDictItem;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.entity.EsStation;
import com.zdkj.subway.support.entity.SwPmTicketCardTypeParam;
import com.zdkj.subway.support.service.SwPmTicketCardTypeParamService;
import com.zdkj.subway.support.util.UploadDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 * 子类型票卡参数表
 *
 * @author cuilei
 * @date 2020-06-01 14:16:02
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swpmticketcardtypeparam")
@Api(value = "swpmticketcardtypeparam", tags = "子类型票卡参数表管理(车票名称)")
public class SwPmTicketCardTypeParamController {

    private final SwPmTicketCardTypeParamService swPmTicketCardTypeParamService;

    /**
     * 分页查询
     *
     * @param page                    分页对象
     * @param swPmTicketCardTypeParam 子类型票卡参数表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwPmTicketCardTypeParamPage(Page page, SwPmTicketCardTypeParam swPmTicketCardTypeParam) {
        return R.ok(swPmTicketCardTypeParamService.page(page, Wrappers.query(swPmTicketCardTypeParam)));
    }

    /**
     * 通过id查询子类型票卡参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmTicketCardTypeParamService.getById(id));
    }

    /**
     * 新增子类型票卡参数表
     *
     * @param swPmTicketCardTypeParam 子类型票卡参数表
     * @return R
     */
    @ApiOperation(value = "新增子类型票卡参数表", notes = "新增子类型票卡参数表")
    //@SysLog("新增子类型票卡参数表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_swpmticketcardtypeparam_add')")
    public R save(@RequestBody SwPmTicketCardTypeParam swPmTicketCardTypeParam) {
        SwPmTicketCardTypeParam defaultObj = ObjectUtils.getDefaultObj(SwPmTicketCardTypeParam.class);
        BeanUtils.copyProperties(swPmTicketCardTypeParam, defaultObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmTicketCardTypeParam));

		QueryWrapper queryWrapper=new QueryWrapper();       
        queryWrapper.eq("sub_name",swPmTicketCardTypeParam.getSubName());       
        queryWrapper.eq("del_flag","0");
        int subNameCount=swPmTicketCardTypeParamService.count(queryWrapper);
        if(subNameCount>0){
            return R.failed(swPmTicketCardTypeParam.getSubName()+" 数据已存在！");
        }
		QueryWrapper queryWrapperEn=new QueryWrapper();       
        queryWrapperEn.eq("sub_en_name",swPmTicketCardTypeParam.getSubEnName());       
        queryWrapperEn.eq("del_flag","0");
        int subEnNameCount=swPmTicketCardTypeParamService.count(queryWrapperEn);
        if(subEnNameCount>0){
            return R.failed(swPmTicketCardTypeParam.getSubEnName()+" 数据已存在！");
        }
        return R.ok(swPmTicketCardTypeParamService.save(defaultObj));
    }

    /**
     * 修改子类型票卡参数表
     *
     * @param swPmTicketCardTypeParam 子类型票卡参数表
     * @return R
     */
    @ApiOperation(value = "修改子类型票卡参数表", notes = "修改子类型票卡参数表")
    //@SysLog("修改子类型票卡参数表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_swpmticketcardtypeparam_edit')")
    public R updateById(@RequestBody SwPmTicketCardTypeParam swPmTicketCardTypeParam) {

		QueryWrapper queryWrapper=new QueryWrapper();       
        queryWrapper.eq("sub_name",swPmTicketCardTypeParam.getSubName());       
        queryWrapper.eq("del_flag","0");
		queryWrapper.ne("id",swPmTicketCardTypeParam.getId());
        int subNameCount=swPmTicketCardTypeParamService.count(queryWrapper);
        if(subNameCount>0){
            return R.failed(swPmTicketCardTypeParam.getSubName()+" 数据已存在！");
        }		
        return R.ok(swPmTicketCardTypeParamService.updateById(swPmTicketCardTypeParam));
    }

    /**
     * 通过id删除子类型票卡参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除子类型票卡参数表", notes = "通过id删除子类型票卡参数表")
    //@SysLog("通过id删除子类型票卡参数表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_swpmticketcardtypeparam_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmTicketCardTypeParamService.removeById(id));
    }

    /**
     * 导入子类型票卡参数
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入子类型票卡参数", notes = "导入子类型票卡参数")
    //@SysLog("导入子类型票卡参数")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwPmTicketCardTypeParam.class,
                    new UploadDataListener(swPmTicketCardTypeParamService, SwPmTicketCardTypeParam.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }

    /**
     * 根据主类型查子类型参数接口
     * @param mainEnName
     * @return
     */
    @ApiOperation(value = "根据主类型查子类型参数接口", notes = "根据主类型查子类型参数接口")
    //@SysLog("根据主类型查子类型参数接口")
    @GetMapping("/dict/{mainEnName}")
    public R getParamByMainType(@PathVariable String mainEnName) {
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>query()
                .lambda().eq(SwPmTicketCardTypeParam::getMainEnName, mainEnName)
                .eq(SwPmTicketCardTypeParam::getStatus, CommonConstants.ENABLE));
        return R.ok(list);
    }
    /**
     * 根据主类型查子类型参数接口(包括未启用状态的)
     * @param mainEnName
     * @return
     */
    @ApiOperation(value = "根据主类型查子类型参数接口", notes = "根据主类型查子类型参数接口")
    //@SysLog("根据主类型查子类型参数接口")
    @GetMapping("/dictAll/{mainEnName}")
    public R getParamByMainTypeOfAll(@PathVariable String mainEnName) {
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>query()
                .lambda().eq(SwPmTicketCardTypeParam::getMainEnName, mainEnName));
        return R.ok(list);
    }

    @ApiOperation(value = "根据主类型查子类型参数接口", notes = "根据主类型查子类型参数接口")
    //@SysLog("根据主类型查子类型参数接口")
    @GetMapping("/dictRP")
    public R getDictByRP() {
        String mainEnName = "YXCP";
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>query()
                .lambda().eq(SwPmTicketCardTypeParam::getMainEnName, mainEnName)
                .in(SwPmTicketCardTypeParam::getSubEnName, "YRP,SRP,ZLLJSRP".split(","))
                .last("ORDER BY FIELD(SUB_EN_NAME, 'YRP','SRP','ZLLJSRP') "));
        return R.ok(list);
    }

    /**
     * 根据主类型查子类型参数接口(包括未启用状态的)--单程票只返回普通单程票
     * @param mainEnName
     * @return
     */
    @ApiOperation(value = "根据主类型查子类型参数接口单程票只返回普通单程票", notes = "根据主类型查子类型参数接口单程票只返回普通单程票")
    //@SysLog("根据主类型查子类型参数接口")
    @GetMapping("/dictSingleAll")
    public R getParamByMainTypeOfAllSingle(String mainEnName) {       
        if(!"DCP".equals(mainEnName) && StringUtils.isNotBlank(mainEnName)){
            List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>query()
                    .lambda().eq(SwPmTicketCardTypeParam::getMainEnName, mainEnName));
            return R.ok(list);
        }
        if ("DCP".equals(mainEnName)) {
            List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>query()
                    .lambda().eq(SwPmTicketCardTypeParam::getMainEnName, mainEnName)
                    .eq(SwPmTicketCardTypeParam::getSubEnName, "PTDCP"));
            return R.ok(list);
        }
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list();
        return R.ok(list);
    }

    /**
     * 字典根据生产日计划发卡类型查对应的发卡方式
     * @param taskType
     * @return
     */
    @ApiOperation(value = "字典根据生产日计划发卡类型查对应的发卡方式", notes = "字典根据生产日计划发卡类型查对应的发卡方式")
    //@SysLog("字典根据生产日计划发卡类型查对应的发卡方式")
    @GetMapping("/dictPlanAll/{taskType}")
    public R getDictPlanAll(@PathVariable String taskType) {
        List<SysDictItem> list = swPmTicketCardTypeParamService.listIssuingMethod(taskType);
        return R.ok(list);
    }

    /**
     * 字典根据生产日计划发卡类型查对应的发卡方式
     * @param
     * @return
     */
    @ApiOperation(value = "字典根据生产日计划发卡类型查对应的发卡方式", notes = "字典根据生产日计划发卡类型查对应的发卡方式")
    //@SysLog("字典根据生产日计划发卡类型查对应的发卡方式")
    @GetMapping("/dictPlanAll")
    public R getDictPlanOfAll() {
        List<SysDictItem> list = swPmTicketCardTypeParamService.listOfIssuingMethod();
        return R.ok(list);
    }

    /**
     * 根据主类型查子类型参数接口
     * @return
     */
    @ApiOperation(value = "根据主类型查子类型参数接口", notes = "根据主类型查子类型参数接口")
    //@SysLog("根据主类型查子类型参数接口")
    @GetMapping("/dict")
    public R getParamByMain() {
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>lambdaQuery()
                .eq(SwPmTicketCardTypeParam::getStatus, CommonConstants.ENABLE));
        return R.ok(list);
    }

    /**
     * 根据主类型查子类型参数接口(包括未启用状态的)
     * @return
     */
    @ApiOperation(value = "根据主类型查子类型参数接口", notes = "根据主类型查子类型参数接口")
    //@SysLog("根据主类型查子类型参数接口")
    @GetMapping("/dictAll")
    public R getParamByMainOfAll() {
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>lambdaQuery());
        return R.ok(list);
    }

    /**
     * 根据主类型查子类型参数内部接口
     * @param mainEnName
     * @return
     */
    @ApiOperation(value = "根据主类型查子类型参数内部接口", notes = "根据主类型查子类型参数内部接口")
    //@SysLog("根据主类型查子类型参数内部接口")
    @GetMapping("/dictInner/{mainEnName}")
    @Inner
    public R getParamByMainTypeInner(@PathVariable String mainEnName) {
        List<SwPmTicketCardTypeParam> list = swPmTicketCardTypeParamService.list(Wrappers.<SwPmTicketCardTypeParam>query()
                .lambda().eq(SwPmTicketCardTypeParam::getMainEnName, mainEnName)
                .eq(SwPmTicketCardTypeParam::getStatus, CommonConstants.ENABLE));
        return R.ok(list);
    }

    /**
     * 字典查找清分规则
     * @param
     * @return
     */
    @ApiOperation(value = "字典查找清分规则", notes = "字典查找清分规则")
    //@SysLog("字典查找清分规则")
    @GetMapping("/dictClearRulesAll")
    public R getDictClearRulesAll() {
        List<SysDictItem> list = swPmTicketCardTypeParamService.listClearRules();
        return R.ok(list);
    }
    /**
     * 字典查找清分规则备注为a
     * @param
     * @return
     */
    @ApiOperation(value = "字典查找清分规则备注为a", notes = "字典查找清分规则备注为a")
    //@SysLog("字典查找清分规则备注为a")
    @GetMapping("/dictClearRulesAAll")
    public R getDictClearRulesAAll() {
        List<SysDictItem> list = swPmTicketCardTypeParamService.listClearRulesA();
        return R.ok(list);
    }
    /**
     * 字典根据清分规则类型查找对应的子清分规则
     * @param clearRulesId
     * @return
     */
    @ApiOperation(value = "字典根据清分规则类型查找对应的子清分规则", notes = "字典根据清分规则类型查找对应的子清分规则")
    //@SysLog("字典根据清分规则类型查找对应的子清分规则")
    @GetMapping("/dictClearRulesAll/{clearRulesId}")
    public R getDictSubClearRulesAll(@PathVariable String clearRulesId) {
        List<SysDictItem> list = swPmTicketCardTypeParamService.SubClearRulesAll(clearRulesId);
        return R.ok(list);
    }



    /**
     * 字典查找清分规则备注为b
     * @param
     * @return
     */
    @ApiOperation(value = "字典查找清分规则备注为b", notes = "字典查找清分规则备注为b")
    //@SysLog("字典查找清分规则备注为b")
    @GetMapping("/dictClearRulesBAll")
    public R getDictClearRulesBAll() {
        List<SysDictItem> list = swPmTicketCardTypeParamService.listClearRulesB();
        return R.ok(list);
    }


    /**
     * 查es站点
     * @param
     * @return
     */
    @ApiOperation(value = "查es站点", notes = "查es站点")
    @GetMapping("/esStationAll")
    public R getEsStationAll() {
        List<EsStation> list = swPmTicketCardTypeParamService.getEsStationAll();
        return R.ok(list);
    }

	 /**
     * 根据查询条件查es站点
     * @param
     * @return
     */
    @ApiOperation(value = "根据查询条件查es站点", notes = "根据查询条件查es站点")
    @GetMapping("/esStationInfo")
    @Inner
    public R getEsStationInfo(String lineCode,String stationCode) {
        EsStation esStation = swPmTicketCardTypeParamService.getEsStationInfo(lineCode,stationCode);
        return R.ok(esStation);
    }



    /**
     * 字典查找票务设备异常管理-设备类型
     * @param
     * @return
     */
    @ApiOperation(value = "字典查找票务设备异常管理-设备类型", notes = "字典查找票务设备异常管理-设备类型")
    //@SysLog("字典查找票务设备异常管理-设备类型")
    @GetMapping("/dictFaultDeviceType")
    public R getDictFaultDeviceType() {
        List<SysDictItem> list = swPmTicketCardTypeParamService.getDictFaultDeviceType();
        return R.ok(list);
    }


    /**
     * 字典根据设备类型查找对应的票务设备异常管理-设备编号
     * @param deviceTypeId
     * @return
     */
    @ApiOperation(value = "字典根据设备类型查找对应的票务设备异常管理-设备编号", notes = "字典根据设备类型查找对应的票务设备异常管理-设备编号")
    //@SysLog("字典根据设备类型查找对应的票务设备异常管理-设备编号")
    @GetMapping("/dictFaultDeviceType/{deviceTypeId}")
    public R getDictFaultDeviceOrder(@PathVariable String deviceTypeId) {
        List<SysDictItem> list = swPmTicketCardTypeParamService.getDictFaultDeviceOrder(deviceTypeId);
        return R.ok(list);
    }
}
